<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryform" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="部门名称" prop="f0502">
        <el-input v-model="queryParams.f0502" placeholder="请输入部门名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="负责人" prop="f0507">
        <el-select clearable v-model="queryParams.f1801" placeholder="请选择">
          <el-option v-for="item in userList" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['departmentManage:departmentManage:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['departmentManage:departmentManage:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['departmentManage:departmentManage:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="departmentManageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="部门名称" align="center" prop="f0502" />
      <el-table-column label="分配token数" align="center" prop="f0504" />
      <el-table-column label="负责人" align="center" prop="f0507" />
      <el-table-column label="电话" align="center" prop="f0505" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['departmentManage:departmentManage:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['departmentManage:departmentManage:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改部门管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="部门名称" prop="f0502">
          <el-input v-model="form.f0502" placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="负责人" prop="f1801">
          <el-select v-model="form.f1801" @change="handleF1801Change" placeholder="请选择" style="width: 100%;">
            <el-option v-for="item in userList" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电话" prop="f0505">
          <el-input v-model="form.f0505" placeholder="请输入负责人电话" />
        </el-form-item>
        <el-form-item label="分配token数" prop="f0504">
          <el-input v-model="form.f0504" placeholder="请输入分配token数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitform">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, listDepartmentManage, getDepartmentManage, delDepartmentManage, addDepartmentManage, updateDepartmentManage } from "@/api/departmentManage/departmentManage";

export default {
  name: "DepartmentManage",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 部门列表
      f0502s: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 部门管理表格数据
      departmentManageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        f0501: null,
        f0502: null,
        f0503: null,
        f0505: null,
        f1801: null,
        f0507: null
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        f0502: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' }
        ],
        f1801: [
          { required: true, message: '负责人不能为空', trigger: 'blur' }
        ],
        f0505: [
          { required: true, message: '电话不能为空', trigger: 'blur' }
        ],
        f0504: [
          { required: true, message: '分配token数不能为空', trigger: 'blur' }
        ]
      },
      // 负责人列表
      userList: [],
    };
  },
  created() {
    listUser().then(response => {
      response.data.forEach(item => {
        this.userList.push({label: item.f1804, value: item.f1801})
      });;
    }
    )
    this.getList();
  },
  methods: {
    /** 查询部门管理列表 */
    getList() {
      this.loading = true;
      listDepartmentManage(this.queryParams).then(response => {
        this.departmentManageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        f0501: null,
        f0502: null,
        f1801: null,
        f0504: null,
        f0505: null,
        f0507: null
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetform("queryform");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.f0501)
      this.f0502s = selection.map(item => item.f0502)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加部门管理";
      console.log(this.open, this.form)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const f0501 = row.f0501 || this.ids
      getDepartmentManage(f0501).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改部门管理";
      });
    },
    /** 提交按钮 */
    submitform() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.f0501 != null) {
            updateDepartmentManage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDepartmentManage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const f0501s = row.f0501 || this.ids;
      const f0502s = row.f0502 || this.f0502s;
      this.$modal.confirm('是否确认删除部门为"' + f0502s + '"的数据项？').then(function() {
        return delDepartmentManage(f0501s);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 负责人下拉框选中事件
    handleF1801Change(value) {
      console.log(value)
      console.log(this.userList)
      this.form.f0507 = this.userList.find(item => item.value === value ).label;
      console.log(this.form.f0507)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/t05/export', {
        ...this.queryParams
      }, `部门管理导出数据_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
